package com.briup.day16;

import java.util.*;

public class Test_TreeMap02 {
    public static void main(String[] args) {
        //测试linkedhashmap
//        测试存取元素的次序是否一致
        Map<String,Student1> map=new LinkedHashMap<>();
        //2.添加元素
        map.put("010", new Student1("zs", 78));
        map.put("005", new Student1("rose", 82));
        map.put("009", new Student1("lucy", 70));
        map.put("019", new Student1("lucy", 70)); //value可以重
        map.put("002", null);
        map.put(null, new Student1("ww", 67));
        Set<String> keySet1=map.keySet();
        for(String s:keySet1){
            System.out.println(s+" "+map.get(s));
        }
    }
    public static void main01(String[] args) {
        //指定比较器对象 按照name降序，name相同，安装age升序
        Comparator<Student1> comp=new Comparator<Student1>() {
            public int compare(Student1 o1, Student1 o2) {
                int r=o1.getName().compareTo(o2.getName());
                if(r==0){
                    r=o1.getAge()-o2.getAge();
                }
                return (r==0)?0:r;
            }
        };
        Map<Student1,String> map=new TreeMap<Student1,String>(comp);
        map.put(new Student1("zs", 78),"010");
        map.put(new Student1("rose", 82),"005");
        map.put(new Student1("lucy", 79),"009");
        map.put(new Student1("lucy", 79),"009");
        map.put(new Student1("tom", 68),"019");
        map.put(new Student1("tom", 86),"012");
        map.put(new Student1("ww", 67),"002");
        Set<Map.Entry<Student1, String>> entries = map.entrySet();
        for(Map.Entry<Student1,String> entry:entries){
           Student1 key=entry.getKey();
           String value=entry.getValue();
            System.out.println("key: "+key+"value: "+value);
        }

    }
}
class Student1 {
    private String name;
    private int age;
    public Student1() {
    }
    public Student1(String name, int age) {
        this.age = age;
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
//    重写equals和hashCode方法

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Student1)) return false;
        Student1 student = (Student1) o;
        return age == student.age && Objects.equals(name, student.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
